import React, { FC, useEffect, useState } from 'react'
import styles from './RequirementsSummaryMethod.module.scss'
import { useColorVar, useStyles } from '@/hooks/styles'
import { MethodCard } from '@/components/graph/methods/MethodCard/MethodCard'
import { SvgIcon } from '@/components/icons'
import { DataAnalysis } from '@/components/icons/DataAnalysis'
import { Graph, Node } from '@antv/x6'
import { TextAsset } from '@/components/graph/assets/TextAsset/TextAsset'
import { observer } from 'mobx-react'
import { AssetInPort } from '@/components/graph/assets/AssetInPort/AssetInPort'
import {
  IRequirementSummaryCardContext,
  ITextAssetCardContext,
  UUID,
} from '@/store/metaStore'
import { useMetaStore, useWtsStore } from '@/hooks'
import { identity } from '@/utils/basic'

interface IRequirementsSummaryMethod {
  data1?: any
  node?: Node
  graph?: Graph
  cardID?: UUID
  cardIndex?: number
  inputTextAssetCardID?: UUID
}
export const RequirementsSummaryMethod: FC<IRequirementsSummaryMethod> =
  observer(({ node, graph, cardID, cardIndex, inputTextAssetCardID }) => {
    const c = useColorVar()
    const styleClass = useStyles(styles)
    const metaStore = useMetaStore()
    const wtsStore = useWtsStore()

    useEffect(() => {
      const inputPort1CardUUID = (
        metaStore.cardContextArray[cardIndex] as IRequirementSummaryCardContext
      )?.inputTextAssetCardID
      if (inputPort1CardUUID) {
        console.log('inputport1 update', inputPort1CardUUID)
        setInputPort1CardUUID(inputPort1CardUUID)
      }
    }, [metaStore.cardContextArray[cardIndex]]) // TODO

    const [inputPort1CardUUID, setInputPort1CardUUID] =
      useState<UUID>(inputTextAssetCardID)

    const isGenerateButtonFrozen = () => !inputPort1CardUUID // TODO

    const [outputTexts, setOutputTexts] = useState<string[]>([
      '整体造型是流线型',
      '不设计进气口',
    ])

    const summarizeRequirements = async () => {
      const designMethod = await wtsStore.operator.get_method_data(cardID)
      console.log('summarize inPorts', designMethod.inPorts)
      const inPort = designMethod.inPorts[0]
      try {
        inPort.assets = [
          (metaStore.cardContext[inputPort1CardUUID] as ITextAssetCardContext)
            ?.assetID,
        ]
        console.log('summarize inports assets', inPort.assets)
      } catch {
        console.error('summarizeRequirements, inPort not asset')
      }
      await wtsStore.operator.update_method({
        ...designMethod,
        inPorts: [inPort],
      })
      console.log('summarize before run method')
      let retDesignMethod
      try {
        retDesignMethod = await wtsStore.operator.run_method(designMethod.id)
      } catch (e) {
        console.error('summarize run method exception', e)
      }
      console.log('summarize return', retDesignMethod)
      setOutputTexts(
        await Promise.all(
          retDesignMethod.outputs[0]?.goals?.map(
            async ({ id }) => await wtsStore.app.get_asset_data(id)
          )
        )
      )
      console.log('summarize return', retDesignMethod)
    }

    const output = outputTexts.map((text) => (
      <TextAsset key={text} width={'100%'} text={text} isEditable={false} />
    ))

    return (
      <MethodCard
        width={384}
        cardTitle={'需求总结'}
        cardIcon={<SvgIcon icon={DataAnalysis} />}
        node={node}
        graph={graph}
        input={<AssetInPort assetType={'string'} />}
        output={output}
        isButtonDisabled={isGenerateButtonFrozen()}
        onClick={summarizeRequirements}
      ></MethodCard>
    )
  })
